const fs = require('fs');
const path = require('path');

module.exports = (req,res,next) =>{
  const targetFolder = './images/articles'; // 替换为目标文件夹的路径
  const fileName = 'acticle-' + req.params.id; // 替换为要删除的文件名前缀

  fs.readdir(targetFolder, (err, files) => {
    if (err) {
      console.error('Error reading folder:', err);
      return;
    }
  
    // 寻找与给定文件名匹配的文件
    const matchingFiles = files.filter(file => file.startsWith(fileName));
  
    if (matchingFiles.length === 0) {
      console.log('No matching file found');
      return;
    }
  
    // 删除匹配的文件
    matchingFiles.forEach(matchingFile => {
      const filePath = path.join(targetFolder, matchingFile);
      fs.unlink(filePath, err => {
        if (err) {
          console.error('Error deleting file:', err);
        } else {
          next();
        }
      });
    });
  });
}